package com.controller;

import com.domain.User;
import com.tool.MD5;
import com.webservice.LoginWebservice;
import com.webservice.UserWebservice;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@Controller
public class LoginController {

    @Resource
    private LoginWebservice loginWebservice;
    @Resource
    private UserWebservice userWebservice;

    @RequestMapping("/")
    public String index(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return "index";
    }

    @RequestMapping("/toLogin")
    public String toLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return "login2";
    }

    @RequestMapping("/lg")
    @ResponseBody
    public int lg(@RequestBody Map<String,String> map, Model model, HttpServletRequest request, HttpServletResponse response)throws IOException{
//        System.out.println("******");
        System.out.println(map.get("tel"));
        
        int i = 0;

        try {
            User user = loginWebservice.login(map.get("tel"), map.get("password"));
            if (user != null) {
                i = user.getId();
//                System.out.println("00000");
            }  //                System.out.println("1111");

            return i;

        }catch (Exception e){

            return -1;
        }
    }

    @RequestMapping("/login2")
    public String login2(String id, Model model, HttpServletRequest request, HttpServletResponse response)throws IOException{

        HttpSession session = request.getSession();
        User user = userWebservice.findById(Integer.valueOf(id.trim()));
//        System.out.println(id);
        if (user.getAccount_state_id() == 2){
            if (user.getRole_id() == 1){
                session.setMaxInactiveInterval(-1);//设置单位为秒，设置为-1永不过期
                session.setAttribute("user", user);
                return "redirect:/admin/equipStatis";
            }
            else if (user.getRole_id() == 3){
                session.setMaxInactiveInterval(-1);//设置单位为秒，设置为-1永不过期
                session.setAttribute("user", user);
                return "redirect:/approver/equipmentstatis";
            }
            else if (user.getRole_id() == 4){
                session.setMaxInactiveInterval(-1);//设置单位为秒，设置为-1永不过期
                session.setAttribute("user", user);
                return "redirect:/reviewer/equip_statis";
            }
            else if(user.getRole_id() == -1){
                session.setMaxInactiveInterval(-1);
                session.setAttribute("user",user);
                return "redirect:/company/pendingBid";
            }
            else {
                response.setContentType("text/html; charset=UTF-8"); //转码
                PrintWriter out = response.getWriter();
                out.flush();
                out.println("<script>");
                out.println("history.back();");
                out.println("</script>");
                return "login2";
            }
        }
        return "login2";
    }
    @RequestMapping("/error")
    public String error(Model model, HttpServletRequest request){

        return "error";
    }
    @RequestMapping("/inter_error")
    public String inter_error(Model model, HttpServletRequest request){
        return "inter_error";
    }
}
